<?php

namespace app\common\model;

use think\model\relation\HasOne;

/**
 * @property Authentication $authentication
 * @method   self       find($data = null)     找单条记录
 */
class User extends base\User
{
    protected $hidden = ["password", "delete_time"];
    protected $append = ["bind_status_text"];
    //绑定状态，绑定状态，0未绑定，1已解绑，2待审核，3.审核不通过，4已绑定
    /**
     * 0 未绑定
     */
    const BIND_STATUS_UNBIND = 0;
    /**
     * 1 已解绑定
     */
    const BIND_STATUS_DELETE_BIND = 1;
    /**
     * 2 待审核
     */
    const BIND_STATUS_AUDIT = 2;
    /**
     * 3 审核不通过
     */
    const BIND_STATUS_UN_PASS = 3;
    /**
     * 4 已绑定
     */
    const BIND_STATUS_BIND = 4;

    public static function getBindStatusList(): array
    {
        return [
            ["value" => self::BIND_STATUS_UNBIND, "name" => "未绑定"],
            ["value" => self::BIND_STATUS_DELETE_BIND, "name" => "已解绑"],
            ["value" => self::BIND_STATUS_AUDIT, "name" => "待审核"],
            ["value" => self::BIND_STATUS_UN_PASS, "name" => "未通过"],
            ["value" => self::BIND_STATUS_BIND, "name" => "已绑定"],
        ];
    }


    /**
     * 获取绑定状态文字
     * @return string
     */
    public function getBindStatusTextAttr(): string
    {
        $bindType = $this->bind_status;
        $statusName = '';
        switch ($bindType) {
            case self::BIND_STATUS_UNBIND:
                $statusName = '未绑定';
                break;
            case self::BIND_STATUS_DELETE_BIND:
                $statusName = '已解绑';
                break;
            case self::BIND_STATUS_AUDIT:
                $statusName = '待审核';
                break;
            case self::BIND_STATUS_UN_PASS:
                $statusName = '未通过';
                break;
            case self::BIND_STATUS_BIND:
                $statusName = '已绑定';
                break;
        }
        return $statusName;
    }


    protected static $userId = 0;//当前用户的ID

    public static function setUserId($userId)
    {
        self::$userId = $userId;
    }

    /**
     * 我的关注
     * @return HasOne
     */
    public function myFollow(): HasOne
    {
        return $this->hasOne(UserFollow::class, "follow_user_id", "id")->where("user_id", self::$userId);
    }

    /**
     * 关联认证信息
     * @return \think\model\relation\HasOne | Authentication
     */
    public function authentication(): \think\model\relation\HasOne|Authentication
    {
        return $this->hasOne(Authentication::class, "user_id", "id");

    }

    /**
     * 用户关注数量
     * @return int
     * @throws \think\db\exception\DbException
     */
    public function getUserFollowCountAttr()
    {
        return UserFollow::where("user_id", $this->id)->count();
    }

    /**
     * 问题关注数
     * @return int
     * @throws \think\db\exception\DbException
     */
    public function getForumFollowCountAttr()
    {
        return ForumFollow::where("user_id", $this->id)->count();

    }
}